<?php
require_once dirname(__FILE__) . '/../includes/common.inc.php';
require_once dirname(__FILE__) . '/../includes/pdoorm.inc.php';

header('Content-Type: text/plain');
die;
  $now = SimpleDate::fromStamp(time() + LAG);
  $now->hours = 0;
  $now->minutes = 0;
  $now->seconds = 0;

$sql="SELECT UNIX_TIMESTAMP(a.payment_date) as pdate,UNIX_TIMESTAMP(a.payment_date) as cdate,a.* FROM customer_payment as a";
$customerarr = getall($sql,1);

//print_r($customerarr);

 foreach ($customerarr as $onecus)
 {
 	
 	$orderinfo = getOrderInfo($onecus['customer_id']);
 	
 	 $paymentarr = array(
					      'trackId' => MiscUtils::unique(),
					      'cancelled' => 0,
					      'createdDate' => SimpleDate::toStamp($now),
					      'date' => $onecus['pdate'],
					      'amount' => $onecus['payment_amount'],
					      'method' => ( $onecus['payment_method'] == 'c') ? 'cash' : 'bank transfer',
					      'stage' => ($orderinfo['type'] == 'Portrait') ? (($onecus['payment_stage'] == 3) ? 2 : $onecus['payment_stage']) : $onecus['payment_stage'],
					      'userAccount' => '',
					      'orderFlag' => $orderinfo['flag'],
					      'orderTrackId' => $orderinfo['trackId'],
					      'orderId' =>$orderinfo['e_oid'],
					      'customerId' => $orderinfo['customerId'],
					      'status' => 0,
					      'isPdSolved' => 0,
					      'pdSolvedNote' => '',
					      'cancelledDate' => 0,
					      'cancelledOperator' => '',
					      'cancelledMessage' => '',
					      'pdAdmAppr' => 0	
                         );
                         insert('ns_v1payment',$paymentarr);
 	
 } 
 
 
 function insert($table,$arra,$debug='')
 {
    global  $myPdo;
    
              if(!empty($arra))
              {
                $expensevalue = '';
                $fieldsexpense = '';
                
                foreach($arra as $key => $value)
                {
                         $fieldsexpense.= '`'.$key.'`,';
                         $expensevalue .=" '".addslashes($value)."',";                   
                }
                
                //$fieldsexpense .= "cancelledMessage";
                $fieldsexpense =substr($fieldsexpense,0,-1);
                //$expensevalue .= "'0'";
                $expensevalue =substr($expensevalue,0,-1);
                
                $expensesql = " insert into {$table}(".$fieldsexpense.") values(".$expensevalue.")";
                
                if(!empty($debug))
                {
                    echo "EXECUTE SQL:\n".$expensesql."\n";
                    return 0;     
                }else{
                    sql_execute($expensesql);
                    return  lastinsertid();
                }   
              }
    
 }
 



 
 /**
  * 
  * get purchaseitem
  */
  //SELECT a.sample_id,b.`name`,b.price,b.unit,b.type,b.model,a.otherId,a.quantity,(a.quantity*b.price) AS total,'' AS packageInc,0 AS isFree,0 AS urgent,NULL AS order_id,a.e_oid FROM ns_v1packageitem AS a LEFT JOIN  ns_v1sample AS b ON a.sample_id =b.e_oid; 
  
 function getOrderInfo($customer_id)
 {
    global $myPdo;
    
    $sql="select * from ns_v1myorder where customerId = '{$customer_id}'";
    
    $purchaseiteminfo = getall($sql,1);
    
    return $purchaseiteminfo[0];
 }
 
  
 
  /**
   * myPdo 1
   * 
   *
   */
 /**
 * 执行sql
 * */
function sql_execute1($sql){
    global $myPdo1;  
    
    $stmt2 = $myPdo1->prepare($sql);
    $stmt2->execute();

    return;
}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

?>